package src.controller;

import java.rmi.RemoteException;
import java.util.Set;
import javax.swing.DefaultListModel;
import src.domain.User;
import src.resource.DialogResource;
import src.resource.ValidateResource;
import src.resource.DialogResource.windowTypeEnum;
import src.service.ClientService;
import src.view.ManageUsers;

public class ManageUsersController {

	private ClientService system;
	private ManageUsers view;
	
	public ManageUsersController(ClientService system) {
		this.view = ManageUsers.getInstance(this);
		this.system = system;		
	}
	
	public void performSearchUsers() throws RemoteException{
		this.cleanForm();
		this.view.getListModelUsers().removeAllElements();
		view.setListModelUsers(toListModel(system.getUsers(view.getTxtSearch().getText())));
		view.getjListUsers().setModel(view.getListModelUsers());
	}
	
	private DefaultListModel toListModel(Set<User> set){
		DefaultListModel l=new DefaultListModel();
		for(User p:set)
			l.addElement(p);
		return l;
	}
	
	public void performDeleteUser() throws RemoteException{
		if(view.getjListUsers().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","Must select a user.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(system.getMailBoxesByUser(((User)this.view.getjListUsers().getSelectedValue()).getId()).size()!=0){
			DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","You can not delete. This user has mailboxes.",windowTypeEnum.wtInformation);
			return;
		}
		
		system.deleteUser(((User)view.getjListUsers().getSelectedValue()).getId());
		DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","User deleted sucessfully.",windowTypeEnum.wtConfirmation);
		this.cleanForm();
		this.view.getTxtSearch().setText("");
		this.view.getListModelUsers().removeAllElements();
	}
	
	public void performSelectItem() throws RemoteException{
		if(view.getjListUsers().getSelectedValue()==null)
			return;
		this.bindUser();
		
	}
	
	public void performUpdateUser() throws RemoteException{
		
		if(!validateRequiredFields()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must complete the required fields.",windowTypeEnum.wtInformation);
			return;
		}
		if(!validateFieldsLenght()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","The fields can not contain more than 50 characters.",windowTypeEnum.wtInformation);
			return;
		}
		
		/*if(system.getUserByUserName(view.getTxtUserName().getText())!=null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","User name in use. Please enter another user name.",windowTypeEnum.wtInformation);
			return;
		}*/
		
		if(view.getjListUsers().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must select a user.",windowTypeEnum.wtInformation);
			return;
		}
		((User)this.view.getjListUsers().getSelectedValue()).setName(view.getTxtName().getText());
		((User)this.view.getjListUsers().getSelectedValue()).setUserName(view.getTxtUserName().getText());
		((User)this.view.getjListUsers().getSelectedValue()).setCountry(view.getTxtCountry().getText());
		((User)this.view.getjListUsers().getSelectedValue()).setCity(view.getTxtCity().getText());
		((User)this.view.getjListUsers().getSelectedValue()).setAddress(view.getTxtAddress().getText());
		((User)this.view.getjListUsers().getSelectedValue()).setPhoneNumber(view.getTxtPhoneNumber().getText());
		((User)this.view.getjListUsers().getSelectedValue()).setActive(view.getChkActive().isSelected());
		
		
		system.updateUser((User)this.view.getjListUsers().getSelectedValue());
		this.view.getListModelUsers().removeAllElements();
		this.cleanForm();
		this.view.getTxtSearch().setText("");
		
		DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","User updated sucessfully.",windowTypeEnum.wtConfirmation);
		
	}
	
	private void bindUser(){
		User u=(User)view.getjListUsers().getSelectedValue();
		view.getTxtName().setText(u.getName());
		view.getTxtUserName().setText(u.getUserName());
		view.getTxtCountry().setText(u.getCountry());
		view.getTxtCity().setText(u.getCity());
		view.getTxtAddress().setText(u.getAddress());
		view.getTxtPhoneNumber().setText(u.getPhoneNumber());
		view.getChkActive().setSelected(u.isActive());
	}
	
	private void cleanForm(){
		view.getTxtName().setText("");
		view.getTxtUserName().setText("");
		view.getTxtCountry().setText("");
		view.getTxtCity().setText("");
		view.getTxtAddress().setText("");
		view.getTxtPhoneNumber().setText("");
		view.getChkActive().setSelected(false);
	}
	
	private boolean validateRequiredFields(){
		return ValidateResource.validateRequiredField(view.getTxtName().getText()) && ValidateResource.validateRequiredField(view.getTxtUserName().getText());
	}
	
	private boolean validateFieldsLenght(){
		return ValidateResource.validateStringLength(view.getTxtName().getText(), 50) && ValidateResource.validateStringLength(view.getTxtUserName().getText(), 50)
		&& ValidateResource.validateStringLength(view.getTxtCountry().getText(), 50) && ValidateResource.validateStringLength(view.getTxtCity().getText(), 50)
		&&ValidateResource.validateStringLength(view.getTxtAddress().getText(), 50)&& ValidateResource.validateStringLength(view.getTxtPhoneNumber().getText(), 50);
	}

}
